package com.cardbaobao.cardbabyclient.ui.pop;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.model.UserBindCards;
import com.cardbaobao.cardbabyclient.ui.adapter.ChooseRepaymentWayAdapter;

import java.util.List;

/**
 * Created by FHW
 * on 2017/11/08
 */

public abstract class RepaymentPop extends PopupWindow implements View.OnClickListener {

    private List<UserBindCards.DebitCard> repaymentBankList;

    private LinearLayout ll_pay;
    private ListView list_view;
    private TextView tvTitle;
    private TextView tv_money;
    private TextView tv_credit_card;
    private ImageView iv_question;
    private RelativeLayout rl_pay_way;
    private TextView tv_determine;
    private TextView tv_repay_bank;

    private RepaymentCallBack repaymentCallBack;

    public RepaymentPop(final Context context, List<UserBindCards.DebitCard> list) {
        View root = LayoutInflater.from(context).inflate(R.layout.pop_repayment_view, null);
        root.findViewById(R.id.ibt_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        ll_pay = (LinearLayout) root.findViewById(R.id.ll_pay);
        tvTitle = (TextView) root.findViewById(R.id.tv_title);
        tv_money = (TextView) root.findViewById(R.id.tv_money);
        tv_credit_card = (TextView) root.findViewById(R.id.tv_credit_card);
        iv_question = (ImageView) root.findViewById(R.id.iv_question);
        rl_pay_way = (RelativeLayout) root.findViewById(R.id.rl_pay_way);
        tv_determine = (TextView) root.findViewById(R.id.tv_determine);
        tv_repay_bank = (TextView) root.findViewById(R.id.tv_repay_bank);
        list_view = (ListView) root.findViewById(R.id.list_view);
        this.repaymentBankList = list;
        final ChooseRepaymentWayAdapter adapter = new ChooseRepaymentWayAdapter(context, repaymentBankList);
        list_view.setAdapter(adapter);
        list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                for (int i = 0; i < repaymentBankList.size(); i++) {
                    if (i == position) {
                        repaymentBankList.get(i).setChoose(true);
                    } else {
                        repaymentBankList.get(i).setChoose(false);
                    }
                }
                adapter.notifyDataSetChanged();
                ll_pay.startAnimation(AnimationUtils.loadAnimation(context, R.anim.slide_in_left));
                list_view.startAnimation(AnimationUtils.loadAnimation(context, R.anim.slide_out_right));
                ll_pay.setVisibility(View.VISIBLE);
                list_view.setVisibility(View.GONE);
                tvTitle.setText("确定付款");

                if (repaymentCallBack != null) {
                    repaymentCallBack.onItemClick(position);
                }

            }
        });
        iv_question.setOnClickListener(this);
        tv_determine.setOnClickListener(this);
        rl_pay_way.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (repaymentBankList != null && repaymentBankList.size() > 1) {
                    ll_pay.setAnimation(AnimationUtils.loadAnimation(context, R.anim.slide_out_left));
                    list_view.setAnimation(AnimationUtils.loadAnimation(context, R.anim.slide_in_right));
                    ll_pay.setVisibility(View.GONE);
                    list_view.setVisibility(View.VISIBLE);
                    tvTitle.setText("选择付款方式");
                } else {//没有绑卡
                    if (repaymentCallBack != null) {
                        repaymentCallBack.onItemClick(-1);
                    }
                }
            }
        });
        setContentView(root);
        setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        setHeight(WindowManager.LayoutParams.MATCH_PARENT);
        setBackgroundDrawable(new ColorDrawable());
        setAnimationStyle(R.style.CustomBottomAnimateStyle);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    }

    /**
     * 设置标题
     *
     * @param title 标题
     */
    public void setTitle(CharSequence title) {
        if (tvTitle != null && !TextUtils.isEmpty(title)) {
            tvTitle.setText(title);
        }
    }

    public void setMoney(CharSequence money) {
        if (tv_money != null && !TextUtils.isEmpty(money)) {
            tv_money.setText(money);
        }
    }

    public void setCreditCard(CharSequence credit_card) {
        if (tv_credit_card != null && !TextUtils.isEmpty(credit_card)) {
            tv_credit_card.setText(credit_card);
        }
    }

    public void setRepayBank(CharSequence repay_bank) {
        if (tv_repay_bank != null && !TextUtils.isEmpty(repay_bank)) {
            tv_repay_bank.setText(repay_bank);
        }
    }

    public interface RepaymentCallBack {
        void onItemClick(int position);
    }

    public RepaymentCallBack getRepaymentCallBack() {
        return repaymentCallBack;
    }

    public void setRepaymentCallBack(RepaymentCallBack repaymentCallBack) {
        this.repaymentCallBack = repaymentCallBack;
    }
}
